package com.bosssoft.hr.train.mybatis.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.Reader;

public class MyBatisUtil {

    /**
     * 获取 sqlsesion （数据库连接会话）
     * @return
     */
    public static SqlSession getSession(){
        SqlSession session = null;
        try{
            //1 读取配置文件
            Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
            //2 创建sqlSessionFactory对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            //3 获取连接   true 代表事务的自动提交
            session = sqlSessionFactory.openSession(true);
        }catch (Exception e){
            e.printStackTrace();
        }


        return  session;

    }

}
